@isTest

private class testcallplanHospitalHCP {
    
    static testMethod void testTrigger() {
         String rtid;
        Profile p  = [select id from profile where name=: 'System Administrator'];       

         User u3 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new2@testorg1.com');
        insert u3;
        User u2 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new@testorg1.com',managerid=u3.id);
        insert u2;
        
        User u1 =new User (alias = 'standt', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='standarduser@testorg1.com',managerid=u2.id);
        insert u1;

        
        system.runas(u1){
            hospital__c hp=new hospital__c( name='hospital',Nickname__c='testnick', approvalstatus__c='审批通过',active__c=true);
            insert hp;
            HCP__c hcp1=new hcp__c(Name='testhcp1',Hospital__c=hp.id,Active__c=true,class2__c='决策人(DM)');
            insert hcp1;
           MonthlyCallPlan__c month=new MonthlyCallPlan__c(PlanYear__c='2011',PlanMonth__c='6月');
            insert month;
           
 //test EventSync;
            //insert;
            recordtype cprt=[select id from recordtype where sobjecttype=:'callplan__c' and name=:'日常拜访计划'];
            CallPlan__c cp=new CallPlan__c( CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）',
                            CallPurpose__c = '商谈专业评估',  recordtypeid=cprt.id,name='callplanname',
                            CallWorkingtype__c='拜访',CallDate__c=datetime.now()+3,HCP__c=hcp1.id,
                            Monthly_Call_Plan__c=month.id,CallPlanDate__c = date.today());
            insert cp;
            system.debug(cp);
            system.debug('Query Result for : ' + cp.id + ' : ' + [select id from event where whatid=:cp.id]);
           // event event=[select id from event where whatid=:cp.id];
         //   system.assert(event<>null);
             month.ownerid=u2.id;
            update month;
            //update;
            cp.CallWorkingtype__c='本地消费者教育活动';
            
            update cp;
           // event=[select id,subject from event where whatid=:cp.id];
            //system.assert(event.subject=='拜访',event.subject);
            
            //delete;
            cp=[select id from callplan__c where id=:cp.id];
            if(cp<>null){
            string cpid=cp.id;
            cpid=cpid.substring(0,15);
            delete cp;
            list<event> eventl=new list<event>();
         //  eventl= [select id from event where whatid=:cpid];
        //    system.assert(eventl.size()<1);
            }
            
 //test SetLastPlannedDate;
            CallPlan__c cp1=new CallPlan__c(CallPlanDate__c = date.today(),CallMessage__c = 'Pastor-高水平DHA令宝宝拥有真“智”卓“健”（2010年3月至8月）',
                            CallPurpose__c = '商谈专业评估',PlanType__c='计划外',recordtypeid=cprt.id,name='callplanname',CallExecutionstatus__c='是',CallWorkingtype__c='拜访',CallDate__c=datetime.now(),HCP__c=hcp1.id,Monthly_Call_Plan__c=month.id);
            insert cp1;
               
            // system.assert(hcp1.lastvisiteddate__c==cp1.CallPlanDate__c,hcp1.lastvisiteddate__c);
            // system.assert(hcp1.lastplanneddate__c==cp1.CallPlanDate__c,hcp1.lastplanneddate__c);
            // system.assert(hp.lastvisiteddate__c==cp1.CallPlanDate__c,hp.lastvisiteddate__c);
           //  system.assert(hcp1.lastplanneddate__c==cp1.CallPlanDate__c,hcp1.lastplanneddate__c);
             
            
 //test InsertHCPChangeHistory;
            //new hcp;
            HCP__c hcp=new hcp__c();
            hcp.name='testhcp';
            hcp.Hospital__c=hp.id;
            hcp.class2__c='决策人(DM)';
            hcp.approvalstatus__c='等待审批';
            hcp.approvaltype__c = '新增医护人员';
            insert hcp;
                 //subtest hcpEffectiveDateTrigger;
            hcp.effectivedate__c=date.today()+5;
            update hcp;
            hcp=[select approvalstatus__c,id from hcp__c where id=:hcp.id];
           // system.assert(hcp.approvalstatus__c=='审批通过但未生效',hcp.approvalstatus__c);
                //end of test hcpEffectiveDateTrigger;
            hcp.approvalstatus__c='审批通过';
            update hcp;
            hcp=[select id,active__c from hcp__c where id=:hcp.id];
      //      system.assert(hcp.active__c=true,hcp.active__c);
            
            //change hcp class;
            hcp.changetoclass1__c='区域学术带头人(RKOL)';
            hcp.approvalstatus__c='等待审批';
            hcp.approvaltype__c='改变医护人员级别';
            update hcp; //reset hcp;
            
            hcp.effectivedate__c=date.today()+2;
            update hcp;
            hcp=[select approvalstatus__c,id from hcp__c where id=:hcp.id];
           // system.assert(hcp.approvalstatus__c=='审批通过但未生效',hcp.approvalstatus__c);
            hcp.approvalstatus__c='审批通过';
            update hcp;
            hcp=[select id,class1__c from hcp__c where id=:hcp.id];
         //   system.assert(hcp.class1__c=='区域学术带头人(RKOL)');
            
            //test rejective;
            hcp.changetoclass1__c='区域学术带头人(RKOL)';
            hcp.approvalstatus__c='等待审批';
            hcp.approvaltype__c='改变医护人员级别';
            update hcp; //reset hcp;
            
            hcp.approvalstatus__c='审批驳回';
            update hcp;
            hcp=[select id,changetoclass1__c from hcp__c where id=:hcp.id];
         //   system.assert(hcp.changetoclass1__c==null);
            
            //test delete hcp;
            hcp.approvalstatus__c='等待审批';
            hcp.approvaltype__c='删除医护人员';
            update hcp; //reset hcp;
            
            hcp.effectivedate__c=date.today()+4;
            update hcp;
            hcp=[select approvalstatus__c,id from hcp__c where id=:hcp.id];
        //    system.assert(hcp.approvalstatus__c=='审批通过但未生效',hcp.approvalstatus__c);
            hcp.approvalstatus__c='审批通过';
            update hcp;
            hcp=[select id,active__c from hcp__c where id=:hcp.id];
         //   system.assert(hcp.active__c==false,hcp.active__c);            
            
           
            
           delete hcp1;
           delete hp;
         
        }
    }   
     static testMethod void testhospitalTrigger() {
         String rtid;
        Profile p  = [select id from profile where name=: 'System Administrator'];       
        User u1 =new User (alias = 'standt', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='standarduser@testorg1.com');
        insert u1;
         User u2 =new User (alias = 'new', email = 'shlia@isoftstone.com', emailencodingkey='UTF-8', lastname = 'Testing', languagelocalekey = 'en_US', localesidkey='en_US', profileid = p.id, timezonesidkey = 'Asia/Shanghai', username='new@testorg1.com');
        insert u2;
        
        system.runas(u1){
//test InsertHospitalChangeHistory ;  
            //insert hospital;
            hospital__c hp1=new hospital__c(name='testhospital',nickname__c='nickhp',approvaltype__c='新增医院',approvalstatus__c='等待审批');
            insert hp1;
            HCP__c hcp11=new hcp__c();
            hcp11.name='testhcp';
            hcp11.Hospital__c=hp1.id;
            hcp11.class2__c='决策人(DM)';
            hcp11.active__c=true;
            hcp11.approvalstatus__c='';
            hcp11.approvaltype__c = '';
            insert hcp11;
            
            hospital_approve__c hcpa=[select id,approvalstatus__c,effectivedate__c,approvaltype__c from hospital_approve__c where hospital__c=:hp1.id];
            
            hp1.effectivedate__c=date.today()+5;
            update hp1;
          //  hp1=[select id,approvalstatus__c from hospital__c where id=:hp1.id];
         //   system.assert(hp1.approvalstatus__c=='审批通过但未生效',hp1.approvalstatus__c);
            hp1.approvalstatus__c='审批通过';
            update hp1;
           hp1=[select id,active__c from hospital__c where id=:hp1.id];
  //          system.assert(hp1.active__c==true,hp1.active__c);
            
            //hospital rename;
            hp1.approvalstatus__c='等待审批';
            hcpa.approvaltype__c='医院重命名';
            hcpa.namechangeto__c='newname';
            update hp1;//reset hospital;
            update hcpa;
            
            hcpa.effectivedate__c=date.today()+6;
            update hcpa;
      //      hp1=[select id,approvalstatus__c from hospital__c where id=:hp1.id];
     //       system.assert(hp1.approvalstatus__c=='审批通过但未生效',hp1.approvalstatus__c);
            hp1.approvalstatus__c='审批通过';
            update hp1;
      //      hp1=[select id,name from hospital__c where id=:hp1.id];
        //    system.assert(hp1.name=='newname',hp1.name);
            
  /*          //hospital change class;
            hp1.approvalstatus__c='等待审批';
            hcpa.approvaltype__c='改变医院级别';
            hcpa.classchangeto__c='关键';
            update hp1;//reset hospital;
            update hcpa;
            
            hp1.effectivedate__c=date.today()+4;
            update hp1;
     //       hp1=[select id,approvalstatus__c from hospital__c where id=:hp1.id];
   //         system.assert(hp1.approvalstatus__c=='审批通过但未生效',hp1.approvalstatus__c);
            hp1.approvalstatus__c='审批通过';
            update hp1;
   //         hp1=[select id,class__c from hospital__c where id=:hp1.id];
   //         system.assert(hp1.class__c=='关键',hp1.class__c);
            */
            //hospital change onwer;
        /*    
            hp1.approvalstatus__c='等待审批';
            hcpa.approvaltype__c='改变MSR';
            hcpa.ownerchangeto__c=u2.id;
            update hp1;//reset hospital;
            update hcpa;
            
            hp1.effectivedate__c=date.today()+3;
            update hp1;
      //      hp1=[select id,approvalstatus__c from hospital__c where id=:hp1.id];
      //      system.assert(hp1.approvalstatus__c=='审批通过但未生效',hp1.approvalstatus__c);
            hp1.approvalstatus__c='审批通过';
            update hp1;
        //    hp1=[select id,ownerid from hospital__c where id=:hp1.id];
    //        user u=[select alias from user where id=:hp1.ownerid];
       //     system.assert(u.alias =='new',u.alias );
            */
            //hospital rejective;
//            hp1.approvalstatus__c='等待审批';
//            update hp1;//reset hospital;
//            hp1.approvalstatus__c='审批驳回';
//            update hp1;
            
 //UpdateHCPStatus ;         
     //       HCP__c hcp=new hcp__c(Name='testhcp1',Hospital__c=hp1.id,Active__c=true,class2__c='决策人(DM)');
     //       insert hcp;
            
            //hospital delete;
            /*
            hp1.approvalstatus__c='等待审批';
            hcpa.approvaltype__c='删除医院';
            update hp1;//reset hospital;
            update hcpa;
            
            hp1.effectivedate__c=date.today()+1;
            update hp1;*/
     //      hp1=[select id,approvalstatus__c from hospital__c where id=:hp1.id and ownerid=:u2.id];
     //       system.assert(hp1.approvalstatus__c=='审批通过但未生效',hp1.approvalstatus__c);
            // hp1.approvalstatus__c='审批通过';
         //    update hp1;
      //      hp1=[select id,active__c from hospital__c where id=:hp1.id];
        //    system.assert(hp1.active__c ==false);
             //  hp1.active__c=false;
          //  update hp1;
           
            }
     }
}